package com.bruce.springbootDemo.config.aop;

import com.bruce.springbootDemo.annotation.Permission;
import com.bruce.springbootDemo.annotation.UserRoleEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.lang.reflect.Method;

/**
 * Created by bruce on 18/11/29 -- 15:59
 */
@Aspect
@Component
public class AccessAop {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(public * com.bruce.springbootDemo.controller.*.*(..))")
    public void point() {
    }

    @Around("point()")
    public Object before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        StopWatch stopWatch = new StopWatch(getClass().getSimpleName());    // 进程停止看所
        Signature signature = proceedingJoinPoint.getSignature();            // 进程接入点的标识
        Class clazz = Class.forName(signature.getDeclaringTypeName());        // 进程执行类
        String methodName = signature.getName();
//
        Permission permission = null;
        String retureType = null;
//        ResponseEntity responseEntity = null;
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName)) {
                permission = method.getAnnotation(Permission.class);
                Class returnClazz = Class.forName(method.getReturnType().getName());
                retureType = method.getReturnType().getSimpleName();
                break;
            }
        }
        if (permission != null) {
            UserRoleEnum userRoleEnum = permission.value();
            logger.info(userRoleEnum.name());
        }
        if (retureType != null) {
            logger.info(retureType);
//            logger.info(responseEntity.getData().toString());
        }
        return proceedingJoinPoint.proceed();  // 执行目标方法
//        return null;  // 执行目标方法
    }
}
